package com.example.controller;

import com.example.entity.DietRecord;
import com.example.service.DietRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/dietrecord")
public class DietRecordController {
    @Autowired
    private DietRecordService dietRecordService;

@RequestMapping("/list")
    public String list(Model model) {
        List<DietRecord> dietRecords = dietRecordService.getAllDietRecord();
        model.addAttribute("dietRecords", dietRecords);
        return "dietrecord/list";
    }
@GetMapping("/add")
    public String toAddPage() {
    return "dietrecord/add";
    }

    @PostMapping("/add")
    public String add(DietRecord dietRecord) {
        System.out.println("接收到的饮食记录：" + dietRecord);
        dietRecordService.addDietRecord(dietRecord);
        return "redirect:/dietrecord/list";
    }
    @GetMapping("/delete/{dietrecordid}")
    public String delete(@PathVariable("dietrecordid") Integer dietrecordid) {
    dietRecordService.deleteDietRecord(dietrecordid);
        return "redirect:/dietrecord/list";
    }
    @PostMapping("/update")
    public String update(DietRecord dietRecord) {
        dietRecordService.updateDietRecord(dietRecord);
        return "redirect:/dietrecord/list";
    }

    // 获取编辑页面
    @GetMapping("/update/{id}")
    public String edit(@PathVariable("id") Integer id, Model model) {
        model.addAttribute("dietRecord", dietRecordService.getDietRecordById(id));
        return "dietrecord/update";
    }
}
